/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package meuteste;

import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author tiago
 */
public class TelaPingar1 extends javax.swing.JFrame {

    /**
     * Creates new form TelaPingar1
     */
    private PortaSerial portaSerial;
    private TelaSemRotaGerente telaSemRotaGerente;
    private TelaPing2 telaPing2;
    private TelaGerenteSemResposta telaGerenteSemResposta;
    private TelaErroNoFormatoDoProtocolo telaErroNoFormatoDoProtocolo;
    private TelaCoordenadorOcupado telaCoordenadorOcupado;

    public TelaPingar1(PortaSerial portaSerial, TelaGerenteSemResposta telaGerenteSemResposta, TelaSemRotaGerente telaSemRotaGerente, TelaErroNoFormatoDoProtocolo telaErroNoFormatoDoProtocolo, TelaCoordenadorOcupado telaCoordenadorOcupado) {
        initComponents();
        this.portaSerial = portaSerial;
        this.telaGerenteSemResposta = telaGerenteSemResposta;
        telaPing2 = new TelaPing2();
        this.telaSemRotaGerente = telaSemRotaGerente;
        this.telaErroNoFormatoDoProtocolo = telaErroNoFormatoDoProtocolo;
        this.telaCoordenadorOcupado = telaCoordenadorOcupado;
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        textFieldNoPingar = new javax.swing.JTextField();
        botaoPingarTela1 = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        textFieldNoPingar.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));

        botaoPingarTela1.setText("Ok!");
        botaoPingarTela1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseReleased(java.awt.event.MouseEvent evt) {
                botaoPingarTela1MouseReleased(evt);
            }
        });

        jLabel1.setText("Digite aqui o endereço do no que deseja pingar");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(textFieldNoPingar, javax.swing.GroupLayout.DEFAULT_SIZE, 251, Short.MAX_VALUE)
                    .addComponent(botaoPingarTela1, javax.swing.GroupLayout.DEFAULT_SIZE, 251, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addGap(0, 25, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(textFieldNoPingar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(botaoPingarTela1)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void botaoPingarTela1MouseReleased(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_botaoPingarTela1MouseReleased
        this.setVisible(false);
        boolean recebiResposta = false;
        while(!recebiResposta) {
            try {
                String resposta = portaSerial.pingar(new StringBuilder(this.textFieldNoPingar.getText()));
                switch (resposta) {
                    case "iR?t":
                        telaSemRotaGerente.setVisible(true);
                        recebiResposta = true;
                        break;
                    case "iG?t":
                        telaGerenteSemResposta.setVisible(true);
                        recebiResposta = true;
                        break;
                    case "iP?t":
                        telaErroNoFormatoDoProtocolo.setVisible(true);
                        recebiResposta = true;
                        break;
                    case "":
                        System.out.println("Não veio resposta");
                        break;
                    case "iiiiT?t":
                        telaCoordenadorOcupado.setVisible(true);
                        recebiResposta = true;
                        break;
                    default:
                        if (!resposta.matches("iX.*")) {
                            StringBuilder stb = new StringBuilder(resposta);
                            telaPing2.getTextAreaTelaPing2().setText(stb.substring(1, stb.length() - 1));
                            telaPing2.pack();
                            telaPing2.setVisible(true);
                            recebiResposta = true;
                        } else {
                            System.out.println("=== Aguardando ===");
                            Thread.sleep(35000);
                        }
                        break;
                }
            } catch (IOException | InterruptedException ex) {
            }
        }
        textFieldNoPingar.setText("");
    }//GEN-LAST:event_botaoPingarTela1MouseReleased

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /*
         * Set the Nimbus look and feel
         */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /*
         * If Nimbus (introduced in Java SE 6) is not available, stay with the
         * default look and feel. For details see
         * http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(TelaPingar1.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(TelaPingar1.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(TelaPingar1.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(TelaPingar1.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /*
         * Create and display the form
         */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                //      new TelaPingar1().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton botaoPingarTela1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JTextField textFieldNoPingar;
    // End of variables declaration//GEN-END:variables
}
